<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

namespace PHPCan\Cache;

defined('ANS') or die();

if (!function_exists('apc_exists')) {
    public function apc_exists ($key)
    {
        return apc_fetch($key) ? true : false;
    }
}

class Apc implements iCache
{
    private $Debug;
    private $loaded;
    private $settings;

    /**
     * public function __construct ([string $autoglobal], [array $settings])
     *
     * return none
     */
    public function __construct ($autoglobal = '', $settings = array())
    {
        global $Debug;

        $this->Debug = $Debug;
        $this->settings = $settings;

        if ($autoglobal) {
            global $Config;

            $Config->config['autoglobal'][] = $autoglobal;
        }
    }

    /**
    * public function connect (string $type)
    *
    * Connect to memcached server
    *
    * return boolean
    */
    public function connect ($type)
    {
        $this->loaded = extension_loaded('apc');

        return $this->loaded ? $this : false;
    }

    /**
    * public function exists ($key)
    *
    * Check variable exists in memory
    *
    * return boolean
    */
    public function exists ($key)
    {
        return ($this->loaded) ? apc_exists($key) : null;
    }

    /**
    * public function set ($key, $value, [$expire integer])
    *
    * Set a variable into memory
    *
    * return mixed
    */
    public function set ($key, $value, $expire = 3600)
    {
        if ($this->loaded) {
            apc_store($key, deflate64($value), $expire);
        }

        return $value;
    }

    /**
    * public function get ($key)
    *
    * Get a variable from memory
    *
    * return mixed
    */
    public function get ($key)
    {
        return ($this->loaded) ? inflate64(apc_fetch($key)) : null;
    }

    /**
    * public function delete ($key)
    *
    * Delete a variable from memory
    *
    * return boolean
    */
    public function delete ($key)
    {
        return ($this->loaded) ? apc_delete($key) : null;
    }

    /**
    * public function clear (void)
    *
    * Clear APC cache
    *
    * return mixed
    */
    public function clear ()
    {
        if (!$this->loaded) {
            return null;
        }

        apc_clear_cache();
        apc_clear_cache('user');
        apc_clear_cache('opcode');

        return true;
    }

    /**
    * public function expire ($key)
    *
    * Return the time to expire a key
    *
    * return mixed
    */
    public function expire ($key)
    {
        if (!$this->loaded) {
            return null;
        }

        $info = apc_cache_info('user');

        if (!$info['cache_list']) {
            return false;
        }

        foreach ($info['cache_list'] as $entry) {
            if ($entry['info'] !== $key) {
                continue;
            }

            if ($entry['ttl'] == 0) {
                return 0;
            }

            return $entry['creation_time'] + $entry['ttl'];
        }

        return false;
    }
}
